package com.changy.ai_tutor_for_dm.service.impl;

import com.changy.ai_tutor_for_dm.entity.User;
import com.changy.ai_tutor_for_dm.mapper.UsersMapper;
import com.changy.ai_tutor_for_dm.service.LoginService;
import com.changy.ai_tutor_for_dm.tools.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class LoginServiceImpl implements LoginService {

    @Autowired
    private UsersMapper usersMapper;

    @Override
    public String login(User user) {

        User userInfo = usersMapper.search(user.getPhoneNumber(), user.getPassword());
        if (userInfo == null) return null; // 用户不存在

        Map<String, Object> claims = new HashMap<>();
        claims.put("phoneNumber",userInfo.getPhoneNumber());
        claims.put("username",userInfo.getUsername());
        claims.put("cardId",userInfo.getCardId());
        claims.put("password",userInfo.getPassword());
        String token = JwtUtils.generateJwt(claims);
        return token;
    }
}
